﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Controller;
using PersistenceCommons;
using System.Globalization;
using Utils;

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;

namespace DGMTCC_ASM2009
{
    /// <summary>
    /// Interaction logic for Operation.xaml
    /// </summary>
    public partial class Operation : Window
    {
        AppController MainControl;

        CultureInfo culture = CultureInfo.CreateSpecificCulture("pt-BR");
        DateTimeStyles styles = DateTimeStyles.None;

       
        public Operation(AppController appControl)
        {
            InitializeComponent();
            MainControl = appControl;
        }

        
        private void btnAnalisarClick(object sender, RoutedEventArgs e)
        {
           
            
                     
            DateTime dateResult = new DateTime();

            string dateString = txtDiaFab.Text + "/" + txtMesFab.Text + "/" + txtAnoFab.Text;

            if (txtCodPeca.Text.Trim() != "" && txtCodSerialNumb.Text.Trim() != "")
            {
                if (DateTime.TryParse(dateString, culture, styles, out dateResult))
                {
                    txtStatusText.Text += "Iniciando sincronismo com o hardware...\n";

                    AnalysisStatus AnalResult = new AnalysisStatus();

                    AnalResult = MainControl.startAnalysis();
                    
                    if (AnalResult.Equals(AnalysisStatus.AnalysisStarted))
                    {
                        txtStatusText.Text += "Sequência de sincronismo realizada com sucesso.\nIniciando comando de início de análise.\n";
                    }

                    if (AnalResult.Equals(AnalysisStatus.AnalysisStarted))
                    {

                        AnalysisObject AnalysedObject = new AnalysisObject();
                           AnalysedObject = MainControl.Analysis(DateTime.Now, txtCodPeca.Text, txtCodSerialNumb.Text);

                        

                        if (AnalysedObject.analysisObjectComponent.Count > 0)
                        {

                            txtStatusText.Text += "Resultados:\n\n";

                            txtStatusText.Text += AnalysedObject.analysisObjectPID + "\n";
                            txtStatusText.Text += AnalysedObject.analysisObjectPackagePID + "\n";

                            foreach (AnalysisObjectComponent aoc in AnalysedObject.analysisObjectComponent)
                            {
                                txtStatusText.Text += aoc.analysisObjectComponentPID.ToString() + "\n";
                                txtStatusText.Text += aoc.analysisQualityResultP1.ToString() + "\n";
                                txtStatusText.Text += aoc.analysisQualityResultP2.ToString() + "\n\n";
                                
                            }
                            
                            txtStatusText.Text += "Resultados retornados com sucesso\n";

                        }

                        else 
                        {
                            txtStatusText.Text += "Erro de análise: Amostra não detectada.\n";
                        }

                    }
                    else
                    {
                        txtStatusText.Text += "Sequência de sincronismo falhou. Erro retornado...";
                    }
                }
                else
                {
                    MessageBox.Show("Insira a data corretamente e no formato dd/mm/aaaa");
                }

            }
            else
            {
                MessageBox.Show("Preencha os todos os campos com os dados da amostra");
            }

            
        }

        private void btnVisualizar_Click(object sender, RoutedEventArgs e)
        {

            DateTime dateResult = new DateTime();

            IList<AnalysisObject> analysisObjects = new List<AnalysisObject>();

            bool date = false;
            bool serial = false;
            bool code = false;

            
            if (dayAnalyzed.Text.Trim()!="" & monthAnalyzed.Text.Trim()!="" & yearAnalyzed.Text.Trim()!="")
            {
                string dateString = dayAnalyzed.Text + "/" + monthAnalyzed.Text + "/" + yearAnalyzed.Text;
                if (DateTime.TryParse(dateString, culture, styles, out dateResult))
                {
                    date = true;
                }

                else
                {
                    MessageBox.Show("Data Incorreta. Entre com data válida.");
                }
            
            }

            if (txtPackAnlyzed.Text.Trim() != "")
            {
                code = true;
            }

            if (txtNumSerialAnalyzed.Text.Trim() != "")
            {
                serial = true;
            }

            if (date & serial & code)
            {
                analysisObjects = MainControl.retrieveAnalyzedObject(txtPackAnlyzed.Text, txtNumSerialAnalyzed.Text, dateResult);
            }

            if (!date & serial & code)
            {
                analysisObjects = MainControl.retrieveAnalyzedObjectNoDate(txtPackAnlyzed.Text, txtNumSerialAnalyzed.Text);
            }

            if (date & !serial & !code)
            {
                analysisObjects = MainControl.retrieveAnalyzedObjectByFactoryDate(dateResult);
            }


            if (!date & serial & !code)
            {
                analysisObjects = MainControl.retrieveAnalyzedObjectBySerial(txtNumSerialAnalyzed.Text);
            }

            if (!date &! serial & code)
            {
                analysisObjects = MainControl.retrieveAnalyzedObjectPackagePID(txtPackAnlyzed.Text);
            }

            if (analysisObjects.Count > 0)
            {
                ObjectAnalised view = new ObjectAnalised(analysisObjects);
                view.Show();
            }
            else
            {
                MessageBox.Show("Nenhum resultado encontrado.");
            }
           
            
        }
    }
}

